package com.jixing.rengran.huaji.ui.activity.base;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.TextView;

import com.jixing.rengran.huaji.R;
import com.jixing.rengran.huaji.mvp.presenter.BasePresenter;
import com.jixing.rengran.huaji.mvp.view.BaseView;

import butterknife.ButterKnife;


/**
 * Created by l5544 on 2017/11/6.
 */

public abstract class BaseActivity<V extends BaseView,P extends BasePresenter> extends AppCompatActivity {
    private boolean enableTitle=true;
    private AlphaAnimation mHideAnimation   = null;
    private AlphaAnimation mShowAnimation   = null;
    protected P mPresenter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        mPresenter = getPresenter();//创建Presenter
        mPresenter.attachView((V) this);
        init();
        if(enableTitle)
            ((TextView)findViewById(R.id.title_text)).setText(getTitleText());
    }

    /**
     * View渐隐动画效果
     */
    public void setHideAnimation(View view, int duration ){
        if( null == view || duration < 0 ){
            return;
        }
        if( null != mHideAnimation ){
            mHideAnimation.cancel( );
        }
        mHideAnimation = new AlphaAnimation(1.0f, 0.0f);
        mHideAnimation.setDuration( duration );
        mHideAnimation.setFillAfter( true );
        view.startAnimation(mHideAnimation);
    }

    /**
     * View渐现动画效果
     */
    public void setShowAnimation( View view, int duration ){
        if( null == view || duration < 0 ){
            return;
        }
        if( null != mShowAnimation ){
            mShowAnimation.cancel( );
        }
        mShowAnimation = new AlphaAnimation(0.0f, 1.0f);
        mShowAnimation.setDuration( duration );
        mShowAnimation.setFillAfter( true );
        view.startAnimation( mShowAnimation );
    }
    protected void go(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
        finish();
    }

    protected void start(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }
    public void setEnableTitle(boolean enableTitle){
        this.enableTitle=enableTitle;
    }
    public abstract int getLayout();
    public abstract void init();
    public abstract String getTitleText();
    protected abstract P getPresenter();
}
